package com.riko.comic.module.comic.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.riko.comic.core.result.Result;
import com.riko.comic.module.comic.bean.po.Comic;
import com.riko.comic.module.comic.bean.po.Tag;
import com.riko.comic.module.comic.service.IComicService;
import com.riko.comic.module.comic.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author xt
 * @since 2021/11/01
 */
@Controller
@RequestMapping("/comics")
public class ComicsController {

    @Autowired
    private IComicService comicService;

    @Autowired
    private ITagService tagService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        List<Tag> tagList = tagService.list();
        request.setAttribute("tagList", tagList);

        long total = comicService.count();
        request.setAttribute("total", total);

        return "index";
    }

    @RequestMapping("/indexPaging")
    public void indexPaging(HttpServletResponse response, @RequestParam(required = false) Integer index, @RequestParam(required = false) Integer pageSize) throws IOException {
        if (index == null) {
            index = 1;
        }
        if (pageSize == null) {
            pageSize = 1;
        }

        List<Comic> comicList = comicService.list(Wrappers.lambdaQuery(Comic.class).last("limit " + (index - 1) * pageSize + "," + pageSize));

        response.getWriter().print(MAPPER.writeValueAsString(comicList));
    }

    @RequestMapping("/queryComic")
    public String queryComic(HttpServletRequest request, @RequestParam String comicName) {
        long total = comicService.count(Wrappers.lambdaQuery(Comic.class).like(Comic::getName, comicName));
        request.setAttribute("total", total);
        request.setAttribute("comicName",comicName);

        return "queryComic";
    }

    @RequestMapping("/queryComicPaging")
    public void queryComicPaging(HttpServletResponse response, @RequestParam String comicName, @RequestParam(required = false) Integer index, @RequestParam(required = false) Integer pageSize) throws IOException {
        if (index == null) {
            index = 1;
        }
        if (pageSize == null) {
            pageSize = 1;
        }

        List<Comic> comicList = comicService.list(Wrappers.lambdaQuery(Comic.class).like(Comic::getName, comicName).last("limit " + (index - 1) * pageSize + "," + pageSize));

        response.getWriter().print(MAPPER.writeValueAsString(comicList));

    }

    @RequestMapping("/queryAuth")
    public String queryAuth(HttpServletRequest request, @RequestParam String auth) {
        long total = comicService.count(Wrappers.lambdaQuery(Comic.class).like(Comic::getAuth, auth));
        request.setAttribute("total", total);
        request.setAttribute("auth",auth);

        return "queryAuth";
    }

    @RequestMapping("/queryAuthPaging")
    public void queryAuthPaging(HttpServletResponse response, @RequestParam String auth, @RequestParam(required = false) Integer index, @RequestParam(required = false) Integer pageSize) throws IOException {
        if (index == null) {
            index = 1;
        }
        if (pageSize == null) {
            pageSize = 1;
        }

        List<Comic> comicList = comicService.list(Wrappers.lambdaQuery(Comic.class).like(Comic::getAuth, auth).last("limit " + (index - 1) * pageSize + "," + pageSize));

        response.getWriter().print(MAPPER.writeValueAsString(comicList));

    }

    @RequestMapping("/queryTag")
    public String queryTag(HttpServletRequest request, @RequestParam Integer tag) {
        List<Tag> tagList = tagService.list();
        request.setAttribute("tagList", tagList);

        long total = comicService.queryCountByTag(tag);
        request.setAttribute("total", total);
        request.setAttribute("tag",tag);

        return "queryTag";
    }

    @RequestMapping("/queryTagPaging")
    public void queryTagPaging(HttpServletResponse response, @RequestParam Integer tag, @RequestParam(required = false) Integer index, @RequestParam(required = false) Integer pageSize) throws IOException {
        if (index == null) {
            index = 1;
        }
        if (pageSize == null) {
            pageSize = 1;
        }

        List<Comic> comicList = comicService.queryByTag(tag,index,pageSize);

        response.getWriter().print(MAPPER.writeValueAsString(comicList));

    }
}
